package com.zyb.eduservice.controller.consumer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyb.commonutils.JwtUtils;
import com.zyb.commonutils.R;
import com.zyb.commonutils.ordervo.CourseWebVoOrder;
import com.zyb.eduservice.client.OrderClient;
import com.zyb.eduservice.entity.EduCourse;
import com.zyb.eduservice.entity.chapter.ChapterVo;
import com.zyb.eduservice.entity.frontvo.CourseFrontVo;
import com.zyb.eduservice.entity.frontvo.CourseWebVo;
import com.zyb.eduservice.service.EduChapterService;
import com.zyb.eduservice.service.EduCourseService;
import com.zyb.servicebase.exception.GuliException;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;

//@CrossOrigin
@RestController
@RequestMapping("/eduservice/coursefront")
public class CourseFrontController {

    @Resource
    private EduCourseService courseService;
    @Resource
    private EduChapterService chapterService;
    @Resource
    private OrderClient orderClient;

    //1 分页查询讲师课程的方法
    @PostMapping("getCourseFrontList/{page}/{limit}")
    public R getTeacherFrontList(@PathVariable Long page,
                                 @PathVariable Long limit,
                                 @RequestBody(required = false) CourseFrontVo courseFrontVo) {
        Page<EduCourse> coursePage = new Page<>(page, limit);
        Map<String, Object> map = courseService.getCourseFrontList(coursePage, courseFrontVo);
        //返回分页所有数据
        return R.ok().data(map);
    }

    //2 课程详情的方法
    @GetMapping("getFrontCourseInfo/{courseId}")
    public R ccc(@PathVariable String courseId, HttpServletRequest request) {
        //根据课程id，编写sql语句查询课程信息
        CourseWebVo courseWebVo = courseService.getBaseCourseInfo(courseId);
        //根据课程id查询章节和小节
        List<ChapterVo> allChapterVideo = chapterService.getAllChapterVideo(courseId);
        //远程调用根据用户id和课程ID查询用户是否已经支付过了
        String userId = JwtUtils.getMemberIdByJwtToken(request);
        boolean flag = orderClient.isBuyCourse(courseId, userId);
        return R.ok().data("courseWebVo", courseWebVo).data("allChapterVideo", allChapterVideo).data("flag", flag);
    }

    //根据课程ID获取课程信息order版
    @PostMapping("getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String id) {
        CourseWebVo courseInfo = courseService.getBaseCourseInfo(id);
        CourseWebVoOrder courseWebVoOrder = new CourseWebVoOrder();
        //把courseInfo对象里面值复制给courseWebVoOrder对象
        BeanUtils.copyProperties(courseInfo, courseWebVoOrder);
        return courseWebVoOrder;
    }

    //根据课程名字模糊查询
    @GetMapping("getCourseByName/{title}")
    public R getCourseByName(@PathVariable String title){
        QueryWrapper<EduCourse> wrapper=new QueryWrapper<>();
        wrapper.like("title",title);
        List<EduCourse> courseList = courseService.list(wrapper);
        return R.ok().data("courseList",courseList);
    }
}
